import { ArgumentsHost,Catch,ExceptionFilter } from "@nestjs/common";
import { QueryFailedError, TypeORMError } from "typeorm";

@Catch(TypeORMError)
export class TypeormFilter implements ExceptionFilter {
    catch(exceptioin:TypeORMError,host:ArgumentsHost) {
        console.log(exceptioin)
        const ctx = host.switchToHttp();
        let code = 500
        if(exceptioin instanceof QueryFailedError) {
            code = exceptioin.driverError.errno
        }
        // 响应 请求对象
        const response = ctx.getResponse();
        // const request = Pctx.getRequest();
        // http状态码
        response.status(500).json({
            code: code,
            timestamp: new Date().toISOString(),
            // path: request.url,
            // method: request.method,
            message: process.env.NODE_ENV === 'development'?exceptioin.message:'系统错误'
        });
    }
}